package com.example.demo.Controller;

import com.example.demo.Common.Result;
import com.example.demo.Common.Employee;
import com.example.demo.Proj.EmployeeLoginVO;
import com.example.demo.Proj.EmployeePageParam;
import com.example.demo.Proj.PageReslut;
import com.example.demo.Proj.PasswordEdit;
import com.example.demo.Service.impl.EmployeeServiceImpl;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {
    @Autowired
    private EmployeeServiceImpl employeeService;

    @PostMapping("/login")
    public Result getLogin(@RequestBody Employee e) {
        Employee login = employeeService.getLogin(e);

        //生成JWT令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", login.getId());
        claims.put("username", login.getUsername());

        String jwt = Jwts.builder()
                .setClaims(claims) //自定义内容(载荷)
                .signWith(SignatureAlgorithm.HS256, "itheima") //签名算法
                .setExpiration(new Date(System.currentTimeMillis() + 24 * 3600 * 1000)) //有效期
                .compact();
        return Result.success(new EmployeeLoginVO(login.getId(), login.getUsername(), login.getName(), jwt));
    }

    @PutMapping("editPassword")
    public Result editPassword(@RequestBody PasswordEdit passwordEdit){
        log.info("修改密码：" + passwordEdit);
        employeeService.editPassword(passwordEdit);
        return Result.success();
    }

    @PostMapping("/logout")
    public Result userLogout(){
        log.info("退出");
        return Result.success();
    }

    @GetMapping("/page")
    public Result getEmployeeList(EmployeePageParam employeePageParam) {
        log.info("page参数: " + employeePageParam);
        PageReslut pageReslut = employeeService.getEmployeeList(employeePageParam);
        log.info("pageResult: " + pageReslut);
        return Result.success(pageReslut);
    }

    @PostMapping()
    public Result addEmployee(@RequestBody Employee employee) {
        log.info("增加的employee: {}", employee);
        employeeService.addEmployee(employee);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result queryEmployeeById(@PathVariable Long id) {
        log.info("根据id获取:" + id);
        Employee employee = employeeService.queryEmployeeById(id);
        if (employee != null) {
            return Result.success(employee);
        }else {
            return Result.error("员工没找到");
        }
    }

    @PutMapping()
    public Result editEmployee(@RequestBody Employee employee){
        log.info("要修改的employee:" + employee);
        employeeService.editEmployee(employee);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    public Result enableOrDisableEmployee(@RequestParam Long id, @PathVariable Integer status){
        log.info("要修改的id为: " + id + ", 状态改为: " + status);
        employeeService.enableOrDisableEmployee(id, status);
        return Result.success();
    }

}
